#!/bin/bash

pkg=$1
token=`cat ./MY_TOKEN` #填写自己的token#
count=1
id=NA
init=NA

curl -X GET --header 'Content-Type: application/json;charset=UTF-8' "https://gitee.com/api/v5/repos/src-openeuler/${pkg}/pulls/1?access_token=${token}" > tmp.list
cat tmp.list|grep "id" > /dev/null
if [ $? -ne 0 ];then
  echo "Error!"
  exit
fi
status=`cat tmp.list |sed "s/,/\n/g"|grep state|awk -F'"' '{print $(NF-1)}'`
#寻找合入的第一个PR
while [ "${status}" != "merged" ]
do
    ((count++))
    echo ========================= $pkg ========================================= $count ===================
    curl -X GET --header 'Content-Type: application/json;charset=UTF-8' "https://gitee.com/api/v5/repos/src-openeuler/${pkg}/pulls/${count}?access_token=${token}" > tmp.list
    status=`cat tmp.list |sed "s/,/\n/g"|grep state|awk -F'"' '{print $(NF-1)}'`
    if [ $count -gt 5 ];then
      exit
    fi
done

#获取PR提交人的ID
id=`cat tmp.list |sed "s/,/\n/g"|grep title -A 30|grep login|awk -F'"' '{print $(NF-1)}'|head -1` 
cat tmp.list |sed "s/,/\n/g"|grep title |grep -i init > /dev/null
init=`echo $?`
#从提交中获取changelog邮箱的最后一条
wget https://gitee.com/src-openeuler/${pkg}/pulls/${count}.diff
mail=`cat ${count}.diff |grep "[<>]"|grep -E "com|org"|grep "\*"|tail -1|awk -F"[<>]" '{print $2}'`
echo ${pkg},${id},${mail},${init} >> result.csv

rm tmp.list ${count}.diff
